﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web.Http;
using Wechat.API.Filter;
using Wechat.Domain;
using Wechat.Dto.Request;
using Wechat.Dto.Search;
using Wechat.Manager;
using Wechat.Util.ReturnMessage;
using Wechat.Util.StateCode;
using Wechat.Util.WechatUtil;

namespace Wechat.API.Controllers
{
    [RoutePrefix("api/wechatmemu"), UnUserAuthorize]
    public class MemuController : ApiController
    {
        #region 属性
        public IWechatMemuManager WechatMemuManager { get; set; }
        public IWechatTokenManager WechatTokenManager { get; set; }

        #endregion

        #region 添加微信菜单

        /// <summary>
        /// 添加微信菜单
        /// </summary>
        /// <returns></returns>
        [Route("addmemu"), HttpPost]
        public ResultMessage AddMemu(WechatMemuRequest request)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false
            };
            if (ModelState.IsValid)
            {
                returnMsg = WechatMemuManager.WechatMemuInsert(request);
            }
            else
            {
                returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                returnMsg.Message = "参数校验失败";
            }
            return returnMsg;
        }
        #endregion

        #region 同步微信菜单
        /// <summary>
        /// 同步微信菜单
        /// </summary>
        /// <returns></returns>
        [Route("synmemu"),HttpGet]
        public ResultMessage SynMemu()
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false
            };
            var data = WechatMemuManager.SynMemu();
            if (data != null)
            {
                string json = JsonConvert.SerializeObject(data);
                var result = WechatMsgUtil.GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + WechatTokenManager.GetAccessToken(), json);
                returnMsg.Code = (int)EnumApiStateCode.Success;
                returnMsg.Message = "设置微信菜单请求成功";
                returnMsg.Status = true;
                returnMsg.Data = result;
            }
            return returnMsg;
        }
        #endregion

        #region 获取菜单列表

        /// <summary>
        /// 同步微信菜单
        /// </summary>
        /// <returns></returns>
        [Route("getmemu"), HttpGet]
        public ResultMessage GetMemu()
        {
           return WechatMemuManager.GetMemuList();
        }
        #endregion

        #region 删除微信菜单
        /// <summary>
        /// 删除微信菜单
        /// </summary>
        /// <returns></returns>
        [Route("deletememu"), HttpPost]
        public ResultMessage DeleteMemu(string ids)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false
            };
            if (ModelState.IsValid)
            {
                returnMsg = WechatMemuManager.WechatMemuDeleteById(ids);
            }
            else
            {
                returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                returnMsg.Message = "参数校验失败";
            }
            return returnMsg;
        }
        #endregion

        #region 修改微信菜单

        /// <summary>
        /// 修改微信菜单
        /// </summary>
        /// <returns></returns>
        [Route("updatememu"), HttpPost]
        public ResultMessage UpdateMemu(WechatMemuRequest request)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false
            };
            if (ModelState.IsValid)
            {
                if ((request.WechatId ?? 0) != 0)
                {
                    returnMsg = WechatMemuManager.WechatMemuUpdateById(request);
                }
                else
                {
                    returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                    returnMsg.Message = "参数校验失败";
                }
            }
            else
            {
                returnMsg.Code = (int)EnumApiStateCode.ParameterValidationFailed;
                returnMsg.Message = "参数校验失败";
            }
            return returnMsg;
        }
        #endregion

        #region 获取微信菜单列表
        /// <summary>
        /// 获取微信菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getmemupage"),HttpGet]
        public ResultMessage GetMemuPageList([FromUri]WechatMemuSearch request)
        {
            return WechatMemuManager.GetWechatMemuPageData(request);
        }
        #endregion
    }
}
